package org.cloudie.docs.editor.client.windows;

import org.cloudie.docs.editor.client.behaviors.HasReceiveBehavior;
import org.cloudie.docs.editor.client.commands.generic.WindowMinimizeCommand;
import org.cloudie.docs.editor.client.commands.generic.WindowRemoveCommand;
import org.cloudie.docs.editor.client.commands.generic.WindowRestoreCommand;
import org.cloudie.docs.editor.client.events.CommandEvent;
import org.cloudie.docs.editor.client.widgets.Window;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class GenericWindow extends Window implements HasReceiveBehavior {
	
  //public static final String GADGET_HOST = "http://gadgets.cloudie.org/gadget.html?v=";
  public static final String GADGET_HOST = "/gadget.html?v=";
  
  protected WindowContents contents;
  
  public GenericWindow(String id, String title, WindowContents contents) {
    super(title, new VerticalPanel(), false);
    this.id = id;
    this.contents = contents;
    if (contents != null ) {
      setContents(contents);
    }
    
    closeButton.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        event.stopPropagation();
        event.preventDefault();
    	CommandEvent.fire(new WindowRemoveCommand(GenericWindow.this.getId()));
      }
    });
    
    minimizeButton.setTitle("Minimize to desktop icon");
    minimizeButton.addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
    	if (GenericWindow.this.contents.getType() ==
    		WindowContents.ContentType.FRAME) {
    	  Frame frame = (Frame) GenericWindow.this.getContentWidget();
    	  GenericWindow.this.contents.value = frame.getUrl();
    	}
    	WindowRestoreCommand wrCmd = new WindowRestoreCommand(
    	   null,
    	   GenericWindow.this.getId(),
           GenericWindow.this.getTitle(),
           GenericWindow.this.contents,
           GenericWindow.this.getAbsoluteLeft(),
           GenericWindow.this.getAbsoluteTop(),
           GenericWindow.this.getContentWidth(),
           GenericWindow.this.getContentHeight());
    	WindowMinimizeCommand wmCmd = new WindowMinimizeCommand(
    	    GenericWindow.this.getId(), wrCmd);
        CommandEvent.fire(wmCmd);
      }
    });
  }

  public WindowContents getContents() {
    return contents;
  }
  
  public void setContents(WindowContents contents) {
    this.contents = contents;
    switch (contents.getType()) {
      case STATIC:
        setContentWidget(new Label(contents.getValue()));
        break;
      default:
        Frame frame = new Frame();
        frame.getElement().setPropertyInt("frameBorder", 0);
        frame.getElement().setPropertyInt("marginwidth", 0);
        frame.getElement().setPropertyInt("marginheight", 0);
        frame.addStyleName("Frame");
        frame.setWidth("100%");
        if (contents.getType() == WindowContents.ContentType.GADGET) {
          String url = getGadgetUrl(contents.getValue());
          if (url != null) {
        	url = URL.encodeComponent(url);
            frame.setUrl(GADGET_HOST + url);
          } else {
        	frame.setUrl("about:blank");
          }
        } else {
          frame.setUrl(contents.getValue());
        }
        setContentWidget(frame);
    	break;
    }
  }
  
  private String getGadgetUrl(String code) {
	String prefix = "src=\"";
	int a = code.indexOf(prefix);
	if (a > 0) {
	  a += prefix.length();
	  int b = code.indexOf('"', a);
	  if (b > 0) {
		return code.substring(a, b);
	  }
	}
	return null;
  }
  
  @Override
  public void moveStart() {
    super.resizeStart();
    if (contents.getType() != WindowContents.ContentType.STATIC) {
      setVisibility(getContentWidget().getElement(), "hidden");
    }
  }
  
  @Override
  public void moveEnd() {
    super.moveEnd();
    if (contents.getType() != WindowContents.ContentType.STATIC) {
      setVisibility(getContentWidget().getElement(), "");
    }
  }
  
  @Override
  public void resizeStart() {
    super.resizeStart();
    if (contents.getType() != WindowContents.ContentType.STATIC) {
      setVisibility(getContentWidget().getElement(), "hidden");
    }
  }
  
  @Override
  public void resizeEnd() {
    super.resizeEnd();
    if (contents.getType() != WindowContents.ContentType.STATIC) {
      setVisibility(getContentWidget().getElement(), "");
    }
  }
  
  private native void setVisibility(
      com.google.gwt.user.client.Element el, String visibility) /*-{
    el.style.visibility = visibility;
  }-*/;
  
  public static class WindowContents {
    
    private ContentType type;
    private String value;
    
    public WindowContents(ContentType type, String value) {
      this.type = type;
      this.value = value;
    }
    
    public ContentType getType() {
      return type;
    }
    
    public String getValue() {
      return value;
    }
    
    public enum ContentType {
      STATIC,
      FRAME,
      GADGET
    }
    
  }
  
}
